﻿namespace Sustainalytics.ClientDW.Synchronization
{
    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Threading.Tasks;

    public class PDFGeneratorHttpClient
    {
        private readonly string _url;

        public PDFGeneratorHttpClient(string url)
        {
            this._url = url;
        }

        public async Task<HttpResponseMessage> CancelGenerationAsync()
        {
            using (var httpclient = new HttpClient())
            {
                return await httpclient.PostAsync(string.Format("{0}api/v1/cancelgeneration", this._url), new StringContent(string.Empty)).ConfigureAwait(false);
            }
        }

        public async Task<HttpResponseMessage> GetReportingStatusAsync()
        {
            using (var httpclient = new HttpClient())
            {
                return await httpclient.GetAsync(string.Format("{0}api/v1/reportstatus", this._url)).ConfigureAwait(false);
            }
        }

        public async Task<HttpResponseMessage> StartGenerationAsync(IDictionary<string, string> connectionStrings, bool hasChanges)
        {
            using (var httpclient = new HttpClient())
            {
                var obj = new
                {
                    //CompanyIds = new[] { Guid.Parse("0d5ccf9c-887e-4378-8ca8-b8f9ae299f31"), Guid.Parse("be3e45d7-7367-426b-8ed5-a87f60006d92") },
                    CompanyIds = new Guid[0],

                    // AffectedControversiesCompanies = syncContext.PreviousChanges
                    //    .Where(x => x.AffectedObjectName == ChangedElement.AFFECTED_CONTROVERSY_REPORT)
                    //    .Select(x => x.AffectedDocumentId)
                    //    .ToArray(),
                    // CompanyIds=new Guid[0],
                    AffectedControversiesCompanies = new Guid[0],
                    connectionStrings,
                    GlobalCompactQuarterChanged = hasChanges
                };
                return await httpclient.PostAsJsonAsync(string.Format("{0}api/v1/generate", this._url), obj).ConfigureAwait(false);
            }
        }

        public HttpResponseMessage StartGeneration(IDictionary<string, string> connectionStrings, bool hasChanges)
        {
            using (var httpclient = new HttpClient())
            {
                var obj = new
                {
                    // CompanyIds = new[] { Guid.Parse("6c6845ff-4d9e-4f4d-b920-962af834ef0a") },
                    CompanyIds = new Guid[0],

                    // AffectedControversiesCompanies = syncContext.PreviousChanges
                    //    .Where(x => x.AffectedObjectName == ChangedElement.AFFECTED_CONTROVERSY_REPORT)
                    //    .Select(x => x.AffectedDocumentId)
                    //    .ToArray(),
                    // CompanyIds=new Guid[0],
                    AffectedControversiesCompanies = new Guid[0],
                    connectionStrings,
                    GlobalCompactQuarterChanged = hasChanges
                };
                return httpclient.PostAsJsonAsync(string.Format("{0}api/v1/generate", this._url), obj).Result;
            }
        }
    }
}